//
//  ClockView.m
//  DigitalClock
//
//  Created by Kishikawa Katsumi on 10/04/04.
//  Copyright 2010 Kishikawa Katsumi. All rights reserved.
//

#import "ClockView.h"

@implementation ClockView

@synthesize twentyFourHourTime;
@synthesize flashTimeSeparator;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    }
    return self;
}

- (void)dealloc {
    [dateFormatter release];
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    NSDate *now = [NSDate date];
    
    // update clock images
    NSString *period = nil;
    UIImage *periodImage = nil;
    if (!twentyFourHourTime) {
        [dateFormatter setDateFormat:@"a"];
        period = [dateFormatter stringFromDate:now];
        periodImage = [UIImage imageNamed:[NSString stringWithFormat: @"%@.png", period]];
        
        [dateFormatter setDateFormat:@"h"];
	} else {
        [dateFormatter setDateFormat:@"H"];
	}
	NSString *hour = [dateFormatter stringFromDate:now];
    NSString *leftHour = nil;
    UIImage *leftHourImage = nil;
    NSString *rightHour = nil;
    UIImage *rightHourImage = nil;
    if ([hour length] == 2) {
        leftHour = [NSString stringWithFormat:@"time_%@.png", [hour substringWithRange:NSMakeRange(0, 1)]];
        leftHourImage = [UIImage imageNamed:leftHour];
        rightHour = [NSString stringWithFormat:@"time_%@.png", [hour substringWithRange:NSMakeRange(1, 1)]];
        rightHourImage = [UIImage imageNamed:rightHour];
    } else {
        rightHour = [NSString stringWithFormat:@"time_%@.png", hour];
        rightHourImage = [UIImage imageNamed:rightHour];
    }

    UIImage *timeSeparatorImage = [UIImage imageNamed:@"time_separator.png"];
	
	[dateFormatter setDateFormat:@"mm"];
	NSString *minute = [dateFormatter stringFromDate:now];
    NSString *leftMinute = [NSString stringWithFormat: @"time_%@.png", [minute substringWithRange:NSMakeRange(0, 1)]];
    UIImage *leftMinuteImage = [UIImage imageNamed:leftMinute];
    
    NSString *rightMinute = [NSString stringWithFormat:@"time_%@.png", [minute substringWithRange:NSMakeRange(1, 1)]];
    UIImage *rightMinuteImage = [UIImage imageNamed:rightMinute];
	
	[dateFormatter setDateFormat:@"ss"];
	NSString *second = [dateFormatter stringFromDate:now];
    NSString *leftSecond = [NSString stringWithFormat:@"time_%@s.png", [second substringWithRange:NSMakeRange(0, 1)]];
    UIImage *leftSecondImage = [UIImage imageNamed:leftSecond];
    
    NSString *rightSecond = [NSString stringWithFormat:@"time_%@s.png", [second substringWithRange:NSMakeRange(1, 1)]];
    UIImage *rightSecondImage = [UIImage imageNamed:rightSecond];
	
    // update date images
	[dateFormatter setDateFormat:@"yyyy"];
	NSString *year = [dateFormatter stringFromDate:now];
    NSString *year1 = [NSString stringWithFormat: @"day_%@.png", [year substringWithRange:NSMakeRange(0, 1)]];
    UIImage *year1Image = [UIImage imageNamed:year1];
    
    NSString *year2 = [NSString stringWithFormat:@"day_%@.png", [year substringWithRange:NSMakeRange(1, 1)]];
    UIImage *year2Image = [UIImage imageNamed:year2];
    
    NSString *year3 = [NSString stringWithFormat:@"day_%@.png", [year substringWithRange:NSMakeRange(2, 1)]];
    UIImage *year3Image = [UIImage imageNamed:year3];
    
    NSString *year4 = [NSString stringWithFormat: @"day_%@.png", [year substringWithRange:NSMakeRange(3, 1)]];
    UIImage *year4Image = [UIImage imageNamed:year4];
    
	[dateFormatter setDateFormat:@"MM"];
	NSString *month = [dateFormatter stringFromDate:now];
    NSString *leftMonth = [NSString stringWithFormat:@"day_%@.png", [month substringWithRange:NSMakeRange(0, 1)]];
    UIImage *leftMonthImage = [UIImage imageNamed:leftMonth];
    
    NSString *rightMonth = [NSString stringWithFormat:
                            @"day_%@.png", 
                            [month substringWithRange:NSMakeRange(1, 1)]];
    UIImage *rightMonthImage = [UIImage imageNamed:rightMonth];
    
	[dateFormatter setDateFormat:@"dd"];
	NSString *day = [dateFormatter stringFromDate:now];
    NSString *leftDay = [NSString stringWithFormat:@"day_%@.png", [day substringWithRange:NSMakeRange(0, 1)]];
    UIImage *leftDayImage = [UIImage imageNamed:leftDay];
    
    NSString *rightDay = [NSString stringWithFormat:@"day_%@.png", [day substringWithRange:NSMakeRange(1, 1)]];
    UIImage *rightDayImage = [UIImage imageNamed:rightDay];
    
    UIImage *dateSeparator1Image = [UIImage imageNamed:@"day_separator.png"];
    UIImage *dateSeparator2Image = [UIImage imageNamed:@"day_separator.png"];
    
	[dateFormatter setDateFormat:@"E"];
	NSString *dayOfWeek = [dateFormatter stringFromDate:now];
    UIImage *weekImage = [UIImage imageNamed:[NSString stringWithFormat:@"week_%@.png", dayOfWeek]];

    // draw clock images
    if ([period isEqualToString:@"AM"]) {
        [periodImage drawAtPoint:CGPointMake(30.0f, 36.0f)];
    } else {
        [periodImage drawAtPoint:CGPointMake(58.0f, 36.0f)];
    }
    
    [leftHourImage drawAtPoint:CGPointMake(30.0f, 56.0f)];
    [rightHourImage drawAtPoint:CGPointMake(76.0f, 56.0f)];
    
    if (flashTimeSeparator) { // flash time separator
        if (!showsSeparator) {
            [timeSeparatorImage drawAtPoint:CGPointMake(119.0f, 56.0f)];
        }
        showsSeparator = !showsSeparator;
    } else {
        [timeSeparatorImage drawAtPoint:CGPointMake(119.0f, 56.0f)];
    }
    
    [leftMinuteImage drawAtPoint:CGPointMake(136.0f, 56.0f)];
    [rightMinuteImage drawAtPoint:CGPointMake(182.0f, 56.0f)];
    
    [leftSecondImage drawAtPoint:CGPointMake(227.0f, 73.0f)];
    [rightSecondImage drawAtPoint:CGPointMake(261.0f, 73.0f)];
    
    // draw date images
    [year1Image drawAtPoint:CGPointMake(30.0f, 149.0f)];
    [year2Image drawAtPoint:CGPointMake(52.0f, 149.0f)];
    [year3Image drawAtPoint:CGPointMake(75.0f, 149.0f)];
    [year4Image drawAtPoint:CGPointMake(97.0f, 149.0f)];
    
    [dateSeparator1Image drawAtPoint:CGPointMake(119.0f, 149.0f)];
    
    [leftMonthImage drawAtPoint:CGPointMake(126.0f, 149.0f)];
    [rightMonthImage drawAtPoint:CGPointMake(149.0f, 149.0f)];
    
    [dateSeparator2Image drawAtPoint:CGPointMake(171.0f, 149.0f)];
    
    [leftDayImage drawAtPoint:CGPointMake(178.0f, 149.0f)];
    [rightDayImage drawAtPoint:CGPointMake(201.0f, 149.0f)];
    
    [weekImage drawAtPoint:CGPointMake(229.0f, 175.0f)];
}

- (void)update {
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // toggle flash time separator setting
    twentyFourHourTime = !twentyFourHourTime;
    [self update];
}

@end
